package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.SupplierDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.Role;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * ClassName: SupplierServiceImpl
 * Package: com.atguigu.jxc.service.impl
 *
 * @Description:
 * @Author: xvyy
 * @Create: 2025/2/21 - 10:40
 * @Version: v1.0
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierDao supplierDao;
    @Autowired
    private LogService logService;

    @Override
    public Map<String, Object> list(Integer page, Integer rows, String supplierName) {
        Map<String, Object> map = new HashMap<>();

        int total = supplierDao.getSupplierCount(supplierName);
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Supplier> roles = supplierDao.getSupplierList(offSet, rows, supplierName);

        logService.save(new Log(Log.SELECT_ACTION, "分页查询供应商管理"));

        map.put("total", total);
        map.put("rows", roles);

        return map;
    }

    @Override
    public ServiceVO save(Supplier supplier) {
        // 供应商ID为空，则为新增操作，否则为修改操作
        if (supplier.getSupplierId() == null) {
            Supplier exSupplier = supplierDao.findSupplierByName(supplier.getSupplierName());
            if (exSupplier != null) {
                return new ServiceVO<>(ErrorCode.SUPPLIER_EXIST_CODE, ErrorCode.SUPPLIER_EXIST_MESS);
            }
            logService.save(new Log(Log.INSERT_ACTION, "新增商户:" + supplier.getSupplierName()));
            supplierDao.insertSupplier(supplier);
        } else {
            logService.save(new Log(Log.UPDATE_ACTION, "修改商户:" + supplier.getSupplierName()));
            supplierDao.updateSupplier(supplier, supplier.getSupplierId());
        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

    @Override
    public ServiceVO delete(String supplierIds) {
        try {
            // 去掉前缀 ids=
            String cleanedSupplierIds = supplierIds.replaceFirst("^ids=", "");
            // 解码 URL 编码的字符串
            String decodedSupplierIds = URLDecoder.decode(cleanedSupplierIds, StandardCharsets.UTF_8.toString());
            // 分割字符串
            String[] supplierIdArr = decodedSupplierIds.split(",");
            // 转换为整数列表
            List<Integer> supplierIdList = Arrays.stream(supplierIdArr).map(Integer::parseInt).collect(Collectors.toList());
            for (Integer supplierId : supplierIdList) {
                // 校验供应商是否存在
                Supplier supplier = supplierDao.getSupplierById(supplierId);
                if (supplier == null) {
                    return new ServiceVO<>(ErrorCode.SUPPLIER_DEL_ERROR_CODE, ErrorCode.SUPPLIER_DEL_ERROR_MESS);
                }
                // 删除供应商
                supplierDao.deleteSupplier(supplierId);
                logService.save(new Log(Log.DELETE_ACTION, "删除供应商:" + supplierId));
            }
            return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
        } catch (
                UnsupportedEncodingException e) {
            // 处理解码异常
            return new ServiceVO<>(ErrorCode.DECODE_ERROR_CODE, ErrorCode.DECODE_ERROR_MESS);
        } catch (NumberFormatException e) {
            // 处理转换异常
            return new ServiceVO<>(ErrorCode.PARA_TYPE_ERROR_CODE, ErrorCode.PARA_TYPE_ERROR_MESS);
        }
    }
}
